# Contibutor: Jason Qiu <jasonwqiu@gmail.com>

_realname=charset_normalizer
pkgbase=mingw-w64-python-charset-normalizer
pkgver=2.1.1
pkgrel=1
pkgdesc="A library that helps you read text from an unknown charset encoding"
pkgname=("${MINGW_PACKAGE_PREFIX}-python-charset-normalizer")
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/Ousret/charset_normalizer"
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest"
              "${MINGW_PACKAGE_PREFIX}-python-pytest-cov"
              "${MINGW_PACKAGE_PREFIX}-python-coverage")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/Ousret/charset_normalizer/archive/${pkgver}.tar.gz")
sha256sums=('f4235706cfc5b3e8f3519abbefbca21442e46a073b8e3ab74ec949e181ac9e4c')

prepare() {
    cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"
}

build() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python setup.py build 
}

check() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m pytest
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install --prefix="${MINGW_PREFIX}" \
        --root="${pkgdir}" --optimize=1 --skip-build
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/LICENSE

  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*-script.py; do
    # Remove shebang line
    sed -e '1 { s/^#!.*$// }' -i "${_f}"
  done
}
